﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.Model;

public partial class Police_TranslateProcess_InnerTranslate : System.Web.UI.Page
{
    public String InfoStr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo member = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);

            BindRoles();


            if (!string.IsNullOrEmpty(Request["ID"]))
            {

                string sid = Request["ID"];
                String[] sidArr = sid.Substring(1).Split(',');
                for (int i = 0; i < sidArr.Length; i++)
                {
                    Soldier soldier = new Soldier();
                    SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(sidArr[i]);
                    if (soldierInfo.IsContract)
                    {
                        Caption.Text = "合同制队员基本情况";
                        lblRank.Visible = false;
                        RankName.Visible = false;
                        lblCardID.Text = "编号：";
                        lblDateJoin.Text = "工作时间：";
                    }
                    else
                    {
                        Caption.Text = "现役士兵基本情况";
                        lblCardID.Text = "士兵证号：";
                        lblDateJoin.Text = "入伍时间：";
                    }
                    BindSoldier(soldierInfo);
                    InfoStr += "<tr>" +
                        "<td height=\"25\" valign='top'>" + SoldierName.Text + "</td>" +
                        "<td valign='top'>" + Sex.Text + "</td>" +
                        "<td valign='top'>" + CardID.Text + "</td>" +
                        "<td valign='top'>" + RankName.Text + "</td>" +
                        "<td valign='top'>" + DutyName.Text + "</td>" +
                        "<td valign='top'>" + NationName.Text + "</td>" +

                        "<td valign='top'>" + SpecialityName.Text + "</td>" +
                        "<td valign='top'>" + DepartmentName.Text + "</td>" +
                      "</tr>";
                }
            }
            if (Request["SelectId"] != null)
            {
                Yipusoft.TranslateApprove.DAL.SelectDataContext db = new Yipusoft.TranslateApprove.DAL.SelectDataContext();
                Yipusoft.TranslateApprove.DAL.TranslateSelect select = db.TranslateSelect.Where(o => o.Id == new Guid(Request["SelectId"])).FirstOrDefault();
                lblDepartment.Text = select.TanslateDepartment;
                lblDepartmentId.Text = select.TanslateDepartmentId;
            }

        }

    }
    private void BindRoles()
    {
        ddlRoles.DataSource = Process.GetJumpList(1, 0);
        ddlRoles.DataBind();
    }
    public string GetSoldierID()
    {
        return Request.QueryString["ID"];
    }

    private void BindSoldier(SoldierArchiveInfo soldierInfo)
    {

        SoldierName.Text = soldierInfo.SoldierName;
        CardID.Text = soldierInfo.CardID;
        Sex.Text = soldierInfo.Sex;
        imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : "../../" + soldierInfo.Picture;
        DepartmentName.Text = soldierInfo.DepartmentName;
        NationName.Text = soldierInfo.NativeName;
        Birthday.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
        JoinDate.Text = soldierInfo.JoinTime.Year == 9999 ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
        RankName.Text = soldierInfo.RankName;
        DutyName.Text = soldierInfo.DutyName;
        SpecialityName.Text = soldierInfo.Speciality2 == "" ? soldierInfo.Speciality1Name : soldierInfo.Speciality1Name + "[" + soldierInfo.Speciality2Name + "]";
    }



    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
            departmentList = Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100') or (DepartmentID = '1300017') or (DepartmentID = '1300038')or (DepartmentID = '1300035')");
        else

            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
                departmentList = Department.GetList("DepartmentID like '" + departmentID.Substring(0, 4) + "%'");
            else
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
                    departmentList = Department.GetList("DepartmentID='" + departmentID + "'");

        return departmentList;
    }

    public List<DepartmentInfo> GetDepartmentList()
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        departmentList = Department.GetList("Right(DepartmentID,2)='00'");
        return departmentList;
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        #region 验证
        string ex = "";
        if (WordFile.HasFile)
        {
            ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "doc" && ex.ToLower() == "jpg" && ex.ToLower() == "gif" && ex.ToLower() == "png" && ex.ToLower() == "bmp")
            {
                Prompt.Text = "提示:上传必须为Word文档或图片!";
                return;
            }
            if (WordFile.FileContent.Length / 1024 / 1024 > 5)
            {
                Prompt.Text = "提示:上传文档不可大于5M!";
                return;

            }
        }
        ApplyInfo apply = new ApplyInfo();
        Guid Aid = Guid.Empty; //请示文件ID
        if (WordFile.HasFile)
        {

            string path = Server.MapPath("..\\TranslateApprove\\WordFile\\");
            string word = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + ex;

            WordFile.SaveAs(path + word);
            apply.FilePath = word;
        }
        apply.ATime = DateTime.Now;
        apply.Title = ApplyTitle.Text;

        apply.UserID = User.Identity.Name;
        Aid = Apply.Insert(apply);
        #endregion
        string sid = Request["ID"];
                String[] sidArr = sid.Substring(1).Split(',');
                for (int i = 0; i < sidArr.Length; i++)
                {
                    #region 士兵基本信息

                    Soldier so = new Soldier();

                    SoldierArchiveInfo soldier = so.GetSoldierInfoByID(sidArr[i]);
                    TranslateInfo info = new TranslateInfo();
                    info.DateArmy = soldier.JoinTime.Year == 1 ? DateTime.MaxValue : soldier.JoinTime;
                    info.DateLastModefy = DateTime.Now;
                    info.FromDepartmentID = soldier.DepartmentID;
                    info.FromJingZhong = "SBDD30";
                    info.NewDepartment = lblDepartment.Text;
                    info.OldDepartment = soldier.DepartmentName;
                    info.OldDepartment2 = soldier.DepartmentName;
                    info.Ranker = soldier.RankName;
                    info.Sex = soldier.Sex;
                    info.SID = new Guid(sidArr[i]);
                    info.SoldierName = soldier.SoldierName;
                    info.TID = 3;
                    info.ToDepartmentID = lblDepartmentId.Text;
                    info.ToJingZhong = "SBDD30";
                    Guid Fid = Translate.Insert(info);

                    #endregion

                    #region 上传请示文件

                    
                    //调动流程
                    TranslateInfo TranslateInfo = Translate.GetDetail(Fid);
                    TranslateLogInfo log = new TranslateLogInfo();
                    log.DID = Aid;
                    log.FID = TranslateInfo.ID;
                    log.LastTime = DateTime.Now;
                    if (TranslateLogs.GetLastDetail(TranslateInfo.ID) != null)
                    {
                        log.PID = TranslateProcess.GetNextDetail(TranslateLogs.GetLastDetail(TranslateInfo.ID).PID).ID;

                    }
                    else
                    {


                        log.PID = TranslateProcess.GetFirstDetail(TranslateInfo.TID).ID;
                    }
                    TranslateLogs.Insert(log);

                    #endregion

                    #region 提交与相应的角色


                    try
                    {
                        int PID = Convert.ToInt32(ddlRoles.SelectedValue.ToString());
                        Yipusoft.TranslateApprove.Model.ProcessInfo process = Process.GetModel(PID);
                        SetupLogInfo setup = new SetupLogInfo();
                        setup.FID = Aid;
                        setup.KID = 1;
                        setup.PID = Process.GetModel(1, 0).PID;
                        setup.Remark = "";
                        setup.RTime = DateTime.Now;
                        setup.State = 1;
                        setup.UserID = User.Identity.Name;
                        setup.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                        SetupLog.Add(setup);
                        string where = " Kid=1 and orderid>0 and orderid<" + process.OrderID + "  ";
                        List<Yipusoft.TranslateApprove.Model.ProcessInfo> list = Process.GetList(where);
                        foreach (Yipusoft.TranslateApprove.Model.ProcessInfo aProcess in list)
                        {
                            //请示流程
                            SetupLogInfo setup1 = new SetupLogInfo();
                            setup1.FID = Aid;
                            setup1.KID = 1;
                            setup1.PID = aProcess.PID;
                            setup1.Remark = "";
                            setup1.RTime = DateTime.Now;
                            setup1.State = 4;
                            setup1.UserID = User.Identity.Name;
                            setup1.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                            SetupLog.Add(setup1);
                        }
                    }
                    catch { }


                    #endregion

                    #region 士兵选择添加日志
                    Yipusoft.TranslateApprove.DAL.SelectDataContext db = new Yipusoft.TranslateApprove.DAL.SelectDataContext();
                    Yipusoft.TranslateApprove.DAL.TranslateSelect select = db.TranslateSelect.Where(o => o.Id == new Guid(Request["SelectId"])).FirstOrDefault();
                    Yipusoft.TranslateApprove.DAL.SelectLogs Selectlog = new Yipusoft.TranslateApprove.DAL.SelectLogs() { Id = Guid.NewGuid(), SelectId = select.Id, SetDate = DateTime.Now, SetUserId = User.Identity.Name, State = 3 };
                    db.SelectLogs.InsertOnSubmit(Selectlog);
                    db.SubmitChanges();
                    #endregion
                }
        Response.Redirect("ProcessList.aspx");
    }
}
